//单链表
class SingleLinkedList {
        public int val;
        public SingleLinkedList next;
        public SingleLinkedList head;
        public int usersize;
        SingleLinkedList(int data) {
            this.val=data;
        }
        SingleLinkedList() {

        }
    public void addFirst(int data){
        SingleLinkedList Node=new SingleLinkedList(data);
        Node.next=head;
        head=Node;
        usersize++;
    }
    //尾插法
    public void addLast(int data){
        if(this.head==null){
            addFirst(data);
            return;
        }
        SingleLinkedList cur=head;
            while(cur.next!=null){
                cur=cur.next;
            }
        SingleLinkedList Node=new SingleLinkedList(data);
        cur.next=Node;
        usersize++;
    }
    public boolean addIndex(int index,int data){
            if(index>usersize || index<0) return false;
            if(index==usersize) {
                addLast(data);
                return true;
            }
            if(index==0) {
                addFirst(data);
                return true;
            }
           SingleLinkedList cur=head;
           SingleLinkedList prev = null;
           while(index>0){
               prev=cur;
               cur=cur.next;
               index--;
           }
           SingleLinkedList Node=new SingleLinkedList(data);
           prev.next=Node;
           Node.next=cur;
           usersize++;
           return true;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        SingleLinkedList cur=head;
        while(cur!=null){
            if(cur.val==key) return true;
            cur=cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        SingleLinkedList cur=head;
        SingleLinkedList prev=null;
        while(cur!=null){
            if(cur.val==key) {
                if(prev==null){
                    this.head=head.next;
                    usersize--;
                    return;
                }else{
                    prev.next=cur.next;
                    usersize--;
                    return;
                }
            }
            prev=cur;
            cur=cur.next;
        }
    }
    public void removeAllKey(int key){
        SingleLinkedList cur=head;
        SingleLinkedList prev=null;
        while(cur!=null){
            if(cur.val==key) {
                if(prev==null){
                    this.head=head.next;
                    prev=null;
                }else{
                    prev.next=cur.next;
                }
                usersize--;
                cur=cur.next;
                continue;
            }
            prev=cur;
            cur=cur.next;
        }
    }
    //得到单链表的长度
    public int size(){
            return usersize;
    }
    public void display(){
            SingleLinkedList cur=head;
            while(cur!=null){
                System.out.println(cur.val+" ");
                cur=cur.next;
            }
    }

    public void clear(){
            this.head.next=null;
            this.head=null;
    }
}
//
public class Demo0714_2 {
    public static void main(String[] args) {
        SingleLinkedList mylist=new SingleLinkedList();
        mylist.addLast(1);
        mylist.addLast(1);
        mylist.addLast(1);
        mylist.addLast(2);
        mylist.addLast(3);
        mylist.addLast(4);
        System.out.println(mylist.size());
        mylist.removeAllKey(1);
        System.out.println(mylist.size());

    }

}